package com.hrm.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.hrm.dao.ExtendsMapper;
import com.hrm.dao.RoleActionMapper;
import com.hrm.dao.RoleMapper;
import com.hrm.dao.UserMapper;
import com.hrm.dao.UserRoleMapper;
import com.hrm.entity.Role;
import com.hrm.entity.RoleAction;
import com.hrm.entity.User;
import com.hrm.entity.UserRole;
import com.hrm.service.RoleService;
import com.hrm.util.Page;

public class RoleServiceImpl implements RoleService {
	private static final Log log = LogFactory.getLog(RoleServiceImpl.class); 
	
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;
	@Autowired
	private RoleActionMapper roleActionMapper;
	@Autowired
	private ExtendsMapper extendsMapper;
	
	public RoleActionMapper getRoleActionMapper() {
		return roleActionMapper;
	}

	public void setRoleActionMapper(RoleActionMapper roleActionMapper) {
		this.roleActionMapper = roleActionMapper;
	}

	public ExtendsMapper getExtendsMapper() {
		return extendsMapper;
	}

	public void setExtendsMapper(ExtendsMapper extendsMapper) {
		this.extendsMapper = extendsMapper;
	}

	public UserMapper getUserMapper() {
		return userMapper;
	}

	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}

	public UserRoleMapper getUserRoleMapper() {
		return userRoleMapper;
	}

	public void setUserRoleMapper(UserRoleMapper userRoleMapper) {
		this.userRoleMapper = userRoleMapper;
	}

	public RoleMapper getRoleMapper() {
		return roleMapper;
	}

	public void setRoleMapper(RoleMapper roleMapper) {
		this.roleMapper = roleMapper;
	}

	public List<Role> getRoles(Page page) {
		log.info("****** Do method getRoles(Page page) begin,params info ==== [ page="+page+" ]");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("currentStart", page.getCurrentStart()-1);
		map.put("currentEnd", page.getCurrentStart()+page.getPagesize()-1);
		List<Role> roles=roleMapper.selectRoleByMap(map);
		log.info("****** Do method getRoles(Page page) end,return info ==== [ List<Role>="+roles+" ]");
		return roles;
	}

	public List<Role> getRoles() {
		log.info("****** Do method getRoles() begin,no params info.");
		List<Role> roles=roleMapper.selectAllRole();
		log.info("****** Do method getRoles() end,return info ==== [ List<Role>="+roles+" ]");
		return roles;
	}


	public Role getRoleById(long id) {
		log.info("****** Do method getRoleById(long id) begin,params info ==== [ id="+id+" ]");
		Role role = roleMapper.selectRoleById(id);
		log.info("****** Do method getRoleById(long id) end,return info ==== [ Role="+role+" ]");
		return role;
	}

	public int addRole(Role role) {
		log.info("****** Do method addRole(Role role) begin,params info ==== [ role="+role+" ]");
		int cnts=-1;
		cnts=roleMapper.insertRole(role);
		log.info("****** Do method addRole(Role role) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public int updateRole(Role role) {
		log.info("****** Do method updateRole(Role role) begin,params info ==== [ role="+role+" ]");
		int cnts=-1;
		cnts=roleMapper.updateRole(role);
		log.info("****** Do method updateRole(Role role) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}
	@Transactional
	public int deleteRoleById(long id) {
		log.info("****** Do method deleteRoleById(long id) begin,params info ==== [ id="+id+" ]");
		UserRole userRole = new UserRole();
		userRole.setRoleId((int)id);
		userRoleMapper.deleteUserRole(userRole);
		log.info("delete all user_role records where role's id = "+id);
		int cnts=-1;
		cnts=roleMapper.deleteRoleById(id);
		log.info("delete role record where role's id = "+id);
		log.info("****** Do method deleteRoleById(long id) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public List<User> getUsersNotRoleId(Page page,long id) {
		log.info("****** Do method getUsersNotRoleId(Page page,long id) begin,params info ==== [ page="+page+",id="+id+" ]");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("roleId", id);
		map.put("currentStart", page.getCurrentStart()-1);
		map.put("currentEnd", page.getCurrentStart()+page.getPagesize()-1);
		List<User> users = extendsMapper.getUsersNotRoleId(map);
		log.info("****** Do method getUsersNotRoleId(Page page,long id) end,return info ==== [ List<User>="+users+" ]");
		return users;
	}
	public int getUsersCountsNotRoleId(long id) {
		log.info("****** Do method getUsersCountsNotRoleId(long id) begin,params info ==== [ id="+id+" ]");
		int cnts=-1;
		cnts=extendsMapper.getUsersCountsNotRoleId(id);
		log.info("****** Do method getUsersCountsNotRoleId(long id) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public List<User> getUsersByRoleId(Page page, long id) {
		log.info("****** Do method getUsersByRoleId(Page page,long id) begin,params info ==== [ page="+page+",id="+id+" ]");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("roleId", id);
		map.put("currentStart", page.getCurrentStart()-1);
		map.put("currentEnd", page.getCurrentStart()+page.getPagesize()-1);
		List<User> users = extendsMapper.getUsersByRoleIdMap(map);
		log.info("****** Do method getUsersByRoleId(Page page,long id) end,return info ==== [ List<User>="+users+" ]");
		return users;
	}

	public int getUsersCountsByRoleId(long id) {
		log.info("****** Do method getUsersCountsByRoleId(long id) begin,params info ==== [ id="+id+" ]");
		int cnts=-1;
		cnts=extendsMapper.getUsersCountsByRoleId(id);
		log.info("****** Do method getUsersCountsByRoleId(long id) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public int deleteUserRoleByRoleId(long id,String userids) {
		log.info("****** Do method deleteUserRoleByRoleId(long id,String userids) begin,params info ==== [ id="+id+",userids="+userids+" ]");
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("roleId", id);
		String[] tmp=userids.split(",");
		Integer[] ids = new Integer[tmp.length];
		for(int i=0;i<tmp.length;i++){
			ids[i]=Integer.parseInt(tmp[i]);
		}
		List<Integer> users=Arrays.asList(ids);
		map.put("userids", users);
		int cnts=-1;
		cnts=userRoleMapper.deleteUserRoleByMap(map);
		log.info("****** Do method deleteUserRoleByRoleId(long id,String userids) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}
	
	@Transactional
	public int addUsersToRole(long id, String userids,User user) {
		log.info("****** Do method addUsersToRole(long id, String userids,User user) begin,params info ==== [ id="+id+",userids="+userids+",user="+user+" ]");
		String[] tmp=userids.split(",");
		int cnts=-1;
		for (String s : tmp) {
			UserRole userRole = new UserRole();
			userRole.setCreateDate(new Date());
			userRole.setRoleId((int)id);
			userRole.setCreateUserId(user.getId());
			userRole.setUserId(Integer.parseInt(s));
			userRole.setCreateUserName(user.getUserName());
			cnts+=userRoleMapper.insertUserRole(userRole);
			log.info("insert one record into user_role, param ="+userRole);
		}
		log.info("****** Do method addUsersToRole(long id, String userids,User user) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public List<UserRole> getRolesByUserId(long id) {
		log.info("****** Do method getRolesByUserId(long id) begin,params info ==== [ id="+id+" ]");
		UserRole userRole = new UserRole();
		userRole.setUserId((int)id);
		List<UserRole> userRoles=userRoleMapper.selectUserRole(userRole);
		log.info("****** Do method getRolesByUserId(long id) end,return info ==== [ List<UserRole>="+userRoles+" ]");
		return userRoles;
	}

	public int addActionsToRole(long id, String actionids, User user) {
		log.info("****** Do method addActionsToRole(long id, String actionids, User user) begin,params info ==== [ id="+id+",actionids="+actionids+",user="+user+" ]");
		int cnts=-1;
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("roleId", id);
		cnts = roleActionMapper.deleteRoleActionByMap(map);
		log.info("delete all role_action where role's id = "+id);
		String[] tmp=actionids.split(",");
		for (String s : tmp) {
			RoleAction roleAction = new RoleAction();
			roleAction.setActionId(Integer.parseInt(s));
			roleAction.setCreateDate(new Date());
			roleAction.setRoleId((int)id);
			roleAction.setUserId(user.getId());
			roleAction.setUserName(user.getUserName());
			cnts+=roleActionMapper.insertRoleAction(roleAction);
			log.info("insert one record into role_action, param ="+roleAction);
		}
		log.info("****** Do method addActionsToRole(long id, String actionids, User user) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

}
